// pages/game/game.js
const app = getApp()
const dayjs = app.dayjs
const login_js = require('../../utils/login.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    count_idx: 0,
    first_bit_idx: 1,
    second_bit_idx: 1,
    model_idx: 0,
    count_list: [10, 20, 30, 50, 100, ],
    first_bit_list: [10, 100, 1000, 10000, 100000],
    second_bit_list: [10, 100, 1000, 10000, 100000],
    model: ['+', '-', '×', '÷'],
    test_list: [],
    answer: null,
    first_bit: null,
    secont_bit: null,
    pointer: 0, //计数器
    last_number: '',
    number_pick: '',
    star_time: null,
    score_number: null,
    has_dot: false,
    is_wrong: false,
    is_dark: false,
    is_time: false, //保存时间
    modal_show: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let test_model = wx.getStorageSync('test_model')
    if (!test_model) {
      test_model = {
        count_idx: 0,
        first_bit_idx: 3,
        second_bit_idx: 1,
        model_idx: 3,
        model_number: 0,
      }
    }
    this.setData({
      title: options.title,
      count_idx: test_model.count_idx,
      first_bit_idx: test_model.first_bit_idx,
      second_bit_idx: test_model.second_bit_idx,
      model_idx: test_model.model_idx,
      model_number: test_model.model_idx,
      star_time: dayjs(),
      is_dark: app.global_data.darkness
    })
    wx.setNavigationBarTitle({
      title: options.title,
    })
    this.setTest()
  },

  ...login_js.methods,
  /**
   * 登录时候获取用户信息回调
   */
  async getUserInfo(e) {
    await this.getProfile()
    this.clickModalCancel()
  },
  clickModalCancel() {
    this.setData({
      modal_show: false,
    })
  },
  setTest() {
    if (this.data.pointer < this.data.count_list[this.data.count_idx]) {
      // 清空选择
      this.setData({
        number_pick: '',
        has_dot: false,
      })
      let first_bit_base = this.data.first_bit_list[this.data.first_bit_idx]
      let second_bit_base = this.data.second_bit_list[this.data.second_bit_idx]
      let fb = Math.floor(Math.random() * (first_bit_base * 0.9) + first_bit_base / 10)
      let sb = Math.floor(Math.random() * (second_bit_base * 0.9) + second_bit_base / 10)
      let is_answer
      if (this.data.model_number === 4) {
        this.setData({
          model_idx: Math.floor(Math.random() * 4)
        })
      }
      switch (this.data.model_idx) {
        case 0:
          is_answer = fb + sb
          break
        case 1:
          if (fb < sb) {
            fb = fb + sb
            sb = fb - sb
            fb = fb - sb
          }
          is_answer = fb - sb
          break
        case 2:
          is_answer = fb * sb
          break
        case 3:
          if (fb < sb) {
            fb = fb + sb
            sb = fb - sb
            fb = fb - sb
          }
          if (sb < 1) {
            sb = Math.floor(Math.random() * (fb - sb) + 1)
          }
          is_answer = Math.floor(fb / sb)
          fb = sb * is_answer
          break
        default:
          break
      }
      this.setData({
        pointer: this.data.pointer + 1,
        first_bit: fb,
        second_bit: sb,
        test_list: [...this.data.test_list, {
          fb,
          sb,
          model_idx: this.data.model_idx,
          answer: is_answer
        }],
        answer: is_answer
      })
      // console.log(this.data.test_list)
    } else {
      this.setScore()
    }
  },
  /* 数字输入 */
  numberPick(e) {
    //检测数字长度
    if (this.data.number_pick.length > 10) {
      wx.vibrateLong({
        success: (res) => {},
      })
      return
    }
    let {
      idx,
    } = e.currentTarget.dataset
    // 检测小数点输入
    if (idx == '.') {
      if (this.data.has_dot) {
        return
      } else {
        this.setData({
          has_dot: true
        })
      }
    }
    this.setData({
      last_number: idx,
      number_pick: this.data.number_pick + idx
    })
  },
  /* 退格 */
  backSpace() {
    if (this.data.last_number == '.') {
      this.setData({
        has_dot: false
      })
    }
    let number = this.data.number_pick
    let is_length = number.length
    number = number.slice(0, is_length - 1)
    let is_last = ''
    if (is_length > 1) {
      is_last = number.slice(is_length - 2, is_length - 1)
    } else {
      is_last = number
    }
    this.setData({
      last_number: is_last,
      number_pick: number
    })
  },
  /* 清空输入 */
  clearNumber() {
    this.setData({
      has_dot: false,
      number_pick: ''
    })
  },
  /* 确认答案 */
  confirmAnswer() {
    let has_answer = +this.data.number_pick
    // console.log(this.data.answer)
    if (has_answer === this.data.answer) {
      this.setData({
        is_wrong: false,
      })
      this.setTest()
    } else {
      this.setData({
        is_wrong: true
      })
      wx.vibrateLong({
        success: (res) => {},
      })
    }
  },
  /* 获取成绩 */
  setScore() {
    if (!this.data.is_time) {
      let now_time = dayjs()
      let score_number = now_time - this.data.star_time
      this.setData({
        is_time: true,
        score_number
      })
      wx.setStorageSync('user_test', this.data.test_list)
    }
    // 登录
    // 如果未登录则弹框
    if (!app.global_data.user_data.openid) {
      this.setData({
        modal_show: true
      })
      return
    }
    wx.redirectTo({
      url: `/pages/score/score?title=${this.data.title}&test_count=${this.data.count_list[this.data.count_idx]}&score_number=${this.data.score_number}`,
    })
  }
})